import forEach from 'lodash/forEach';

var hasClass = function(el, className) {
    if (!className) return false;
    if (el.classList) {
        return el.classList.contains(className);
    } else {
        return new RegExp('(^| )' + className + '( |$)', 'gi').test(el.className);
    }
};
var addClass = function(el, className) {
    if (!className) return;
    let classNames = className.split(" ")

    forEach(classNames,cn=>{
        if(!hasClass(el,cn)){
            if (el.classList) {
                el.classList.add(cn);
            } else {
                el.className += ' ' + cn;
            }
        }
    })
    return el;
}

var removeClass = function(el, className) {
    if (!className) return;
    let classNames = className.split(" ")
    forEach(classNames,cn=>{
        if(hasClass(el,cn)){
            if (el.classList) {
                el.classList.remove(className);
            } else {
                el.className = el.className.replace(new RegExp('(^|\\b)' + className.split(' ').join('|') + '(\\b|$)', 'gi'), ' ');
            }
        }
    })
    return el;
}


export {
    hasClass,
    addClass,
    removeClass
}